package com.myNovel.novel.manager;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.myNovel.novel.core.common.constant.CacheConstants;
import com.myNovel.novel.core.common.constant.DatabaseConstants;
import com.myNovel.novel.dao.entity.AuthorInfo;
import com.myNovel.novel.dao.mapper.AuthorInfoMapper;
import com.myNovel.novel.dto.AuthorInfoDto;
import lombok.RequiredArgsConstructor;
import org.checkerframework.checker.units.qual.A;
import org.checkerframework.checker.units.qual.C;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Objects;

/**
 * @Author：Guxx
 * @Package：com.myNovel.novel.manager
 * @Date：2024/4/1 19:56
 * @Filename：AuthorInfoCacheManager
 */
@Component
@RequiredArgsConstructor
public class AuthorInfoCacheManager {
    private AuthorInfoMapper authorInfoMapper;
    /**
     * 查询作者信息并且放入缓存中*/
    @Cacheable(cacheManager = CacheConstants.REDIS_CACHE_MANAGER,
            value = CacheConstants.AUTHOR_INFO_CACHE_NAME,unless = "#result == null")
    public AuthorInfoDto getAuthor(Long userId){
        QueryWrapper<AuthorInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(DatabaseConstants.AuthorInfoTable.COLUMN_USER_ID,userId)
                .last(DatabaseConstants.SqlEum.LIMIT_1.getSql());
        AuthorInfo authorInfo = authorInfoMapper.selectOne(queryWrapper);

        if (Objects.isNull(authorInfo)){
            return null;
        }
        return AuthorInfoDto.builder()
                .id(userId)
                .panName(authorInfo.getPenName())
                .status(authorInfo.getStatus()).build();
    }
    @Cacheable(cacheManager = CacheConstants.REDIS_CACHE_MANAGER,
    value = CacheConstants.AUTHOR_INFO_CACHE_NAME)
    public void evictAuthorCache(){
        //调用此方法自动清除作者缓存
    }


}
